package com.sgcc.lms;

import com.sgcc.lms.bean.Person;
import com.sgcc.lms.bean.Student;
import com.sgcc.lms.config.*;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Map;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-11-27 14:45
 * @Description:
 */
public class MainTest {
    public static void main(String[] args) {

    }

    @Test
    public void config(){
        //将Config组件注入容器，不需要通过注解@Configuration
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
//        Person bean = applicationContext.getBean(Person.class);
//        System.out.println(bean);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }
    }

    @Test
    public void config2(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config2.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();//获取所有bean的名字
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }
        Object person = applicationContext.getBean("person4");
        Object person2 = applicationContext.getBean("person4");
        System.out.println(person);
        System.out.println(person==person2);
    }

    @Test
    public void config3() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config3.class);
        ConfigurableEnvironment environment = applicationContext.getEnvironment();

        String property = environment.getProperty("os.name");
        System.out.println(property);
        String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String name : namesForType) {
            System.out.println(name);
        }

        Map<String, Person> persons = applicationContext.getBeansOfType(Person.class);
        System.out.println(persons);

    }

    @Test
    public void config4() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config4.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }

    }

    @Test
    public void config5() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config5.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }

    }
    @Test
    public void config6() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config6.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }
        Object bean = applicationContext.getBean("color-01");
        System.out.println(bean);

    }
    @Test
    public void config7() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config7.class);

        Object bean = applicationContext.getBean("colorFactoryBean");
        Object bean2 = applicationContext.getBean("colorFactoryBean");
        System.out.println(bean);
        System.out.println(bean2);
        //获取工厂bean本身
        Object bean3 = applicationContext.getBean("&colorFactoryBean");
        System.out.println(bean3);
    }
    @Test
    public void configOfLifeCycle() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigOfLifeCycle.class);
        applicationContext.close();

    }

    @Test
    public void configOfPropertiesValue(){
        //将Config组件注入容器，不需要通过注解@Configuration
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigOfPropertiesValue.class);
        Student bean = applicationContext.getBean(Student.class);
        System.out.println(bean);
    }
}
